# __init__.py 为初始入口文件,工程代码的入口文件.

# 导入动作库常用函数
from ascript.android.action import click,slide,Touch,gesture
# 导入控件检索相关
from ascript.android.node import Selector
# 导入系统相关
from ascript.android import system
# 环境设备相关
from ascript.android.system import R,Device
from ascript.android.action import swipe
from ascript.android.ui import FloatWindow

from time import sleep,time
import re
Dialog.toast("3秒后将隐藏悬浮按钮",dur=1000,x=0,y=0.28,bg_color="#00FFFFFF")
sleep(1)
Dialog.toast("Running...",dur=3000,x=0,y=0.28,bg_color="#00FFFFFF")
#print("3秒后将隐藏悬浮按钮")
sleep(3)
FloatWindow.hide()
# 点击某个地方
# click(100,100)
devicebrand = Device.brand()
print(f"devicebrand = {devicebrand}")

def movebyslide(x,y,plus = 150 ):
    sleep(0.5)
    slide(x, y, x, y - plus, 1000)
    sleep(0.8)
    slide(x, y - plus, x, y, 1000)

def movebytouch():
    print("slide at movebytouch()")
    x = 927
    y = 258
    plus = 200
    sleep(0.5)
    Touch.down(x,y - 150,300)
    sleep(0.2)
    Touch.move(x,y,1500)
    sleep(0.3)
    Touch.up(x,y,300)

def slidenew():
    #Selector().id("com.smile.gifmaker:id/camera_btn_layout").find() #摄像头
    search = Selector().id("com.smile.gifmaker:id/nasa_featured_default_search_view").visible(True).find() or Selector().id("com.smile.gifmaker:id/search_btn").desc("查找").find() # 搜索按钮
    if search:
        x = search.rect.centerX()
        y = search.rect.centerY()
        if x <= 800 and y >= 300:
            return False
        print(f"已找到--搜索按钮，x, y 坐标 = {x}, {y}，即将滑动")
        sleep(0.5)
        Touch.down(x,y,300)
        sleep(0.2)
        Touch.move(x,y+150,1500)
        sleep(0.3)
        Touch.up(x,y+150,300)
        return True
    return False

def swipecore(msg = "准备查找展开按钮"):
    print(msg)
    node = Selector().text("展开").find()
    if node:
        print(f"已找到--展开，即将滑动")
        # x = node.rect.centerX()
        # y = node.rect.centerY()
        # print(f"已找到--展开，x, y 坐标 = {x}, {y}，即将滑动")
        # if y > 300:
        #     #movebyslide(x,y)
        #     movebytouch(x,y,plus = 150)
        # else:
        #     #movebyslide(x,y,plus = -150)
        #     movebytouch(x,y,plus = -150)
        if not slidenew():
            movebytouch()


def floatCtrl():
    while True:
        nodes = Selector().packageName("com.aojoy.airscript").find_all() or []
        if len(nodes) > 0:
            if not Selector().desc("AoJoyLog").find():
                FloatWindow.show()
            elif len(nodes) > 1:
                pass
            elif len(nodes) == 1 and nodes[0].desc == "AoJoyLog":
                FloatWindow.hide()
                break
        else:
            break
        sleep(5)

last_record = None
while True:
    node = Selector().desc("(已|未)点赞，点赞").visible(True).find()
    if node:
        current_record = node.desc
        if re.findall("已点赞",current_record):
            sleep(3)
            continue
        if current_record and current_record != last_record:
            print(f"node.desc = {current_record}")
            last_record = current_record
            swipecore()
    floatCtrl()
    sleep(1)
        #else:
            #sleep(1)
            #continue

        
        
